﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using YTCMS.Core;
using YTCMS.Core.YTModelBinder;
using YTCMS.Data.Infrastructure;

namespace YTCMS.Services
{
    public abstract class BaseService<T> where T : class
    {

        private readonly IRepository<T> _repositoryBase;
        private readonly IUnitOfWork _unitOfWork;
        public BaseService(IRepository<T> repositoryBase, IUnitOfWork unitOfWork)
        {
            this._repositoryBase = repositoryBase;
            this._unitOfWork = unitOfWork;
        }
        public T Add(T t)
        {
            _repositoryBase.Add(t);
            _unitOfWork.Commit();
            return t;
        }
        public T Get(Expression<Func<T, bool>> where)
        {
            return _repositoryBase.Get(where);
        }
        public T Get(Expression<Func<T, bool>> where, bool NoTracking)
        {
            return _repositoryBase.Get(where, NoTracking);
        }

        public IQueryable<T> GetMany(Expression<Func<T, bool>> where)
        {
            var query = _repositoryBase.GetMany(where).AsQueryable();
            return query;
        }

        public void Update(T t)
        {
            _repositoryBase.Update(t);
            _unitOfWork.Commit();
        }
        public void Delete(T t)
        {
            _repositoryBase.Delete(t);
            _unitOfWork.Commit();
        }
        public void Delete(Expression<Func<T, bool>> where)
        {
            _repositoryBase.Delete(where);
            _unitOfWork.Commit();
        }
        public IQueryable<T> PageList(IQueryable<T> t, int pageIndex, int pageSize, out int pcount)
        {
            return _repositoryBase.GetPageList(t, pageIndex, pageSize, out pcount).AsQueryable();
        }
        public IQueryable<T> PageList(int pageIndex, int pageSize, out int pcount)
        {
            return _repositoryBase.GetPageList(pageIndex, pageSize, out pcount).AsQueryable();
        }

        public IQueryable<T> PageList(SearchModel searchModel, int pageIndex, int pageSize, out int pcount)
        {
            return _repositoryBase.PageList(searchModel, pageIndex, pageSize, out pcount).AsQueryable();
        }
        public IQueryable<T> PageList(Expression<Func<T, bool>> where, int pageIndex, int pageSize, out int pcount)
        {
            return _repositoryBase.GetPageList(where, pageIndex, pageSize, out pcount).AsQueryable();
        }

        public IEnumerable<T> GetMany(Expression<Func<T, bool>> where, string includeName)
        {
            return _repositoryBase.GetMany(where, includeName).AsQueryable();
        }

        public IEnumerable<T> GetMany(bool noTracking, Expression<Func<T, bool>> where)
        {
            return _repositoryBase.GetMany(noTracking, where);
        }

        public IEnumerable<T> GetAsyncMany(Expression<Func<T, bool>> where)
        {
            return _repositoryBase.GetAsyncMany(where);
        }

        public T GetAsync(Expression<Func<T, bool>> where)
        {
            return _repositoryBase.GetAsync(where);
        }
        public object ExecuteScalar(string sql)
        {
            return _repositoryBase.ExecuteScalar(sql);
        }
        public int ExecuteSqlCommand(string sql, params object[] obj)
        {
            return _repositoryBase.ExecuteSqlCommand(sql, obj);
        }

        public IQueryable<T> GetMany(Expression<Func<T, bool>> where, Order orderBy, string orderFile)
        {
            return _repositoryBase.GetMany(where, orderBy, orderFile);
        }
    }
}
